package com.zhu.dao;

import com.zhu.pojo.Student;
import com.zhu.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
    private Connection connection;
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;


    public List<Student> getStudent() throws SQLException {
        ArrayList<Student> studentList = new ArrayList<>();

        connection = JDBCUtil.getInstance().getConnection();
        preparedStatement = connection.prepareStatement("select * from student");
        resultSet = preparedStatement.executeQuery();
        Student student ;
        while (resultSet.next()) {
            student = new Student();
            student.setSid(resultSet.getInt("sid"));
            student.setSname(resultSet.getString("sname"));
            student.setCid(resultSet.getInt("cid"));
            student.setHid(resultSet.getInt("hid"));
            studentList.add(student);
        }
        return studentList;
    }
    public Student getStudentById(int sid) throws SQLException {
        connection = JDBCUtil.getInstance().getConnection();
        preparedStatement = connection.prepareStatement("select * from student where sid=?");
        preparedStatement.setInt(1,sid);
        resultSet = preparedStatement.executeQuery();
        Student student = null;
        while (resultSet.next()) {
            student = new Student();
            student.setSid(resultSet.getInt("sid"));
            student.setSname(resultSet.getString("sname"));
            student.setCid(resultSet.getInt("cid"));
            student.setHid(resultSet.getInt("hid"));
        }
        return student;
    }
    public void delStudent(int sid)  {
        connection = JDBCUtil.getInstance().getConnection();
        try {
            preparedStatement = connection.prepareStatement(" DELETE FROM student WHERE sid=?");
            preparedStatement.setInt(1,sid);
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void insertStudent(String sname,int cid,int hid)  {
        connection = JDBCUtil.getInstance().getConnection();
        try {
            preparedStatement = connection.prepareStatement(" insert into student(`sname`,`cid`,`hid`) values (?,?,?)");
            preparedStatement.setString(1,sname);
            preparedStatement.setInt(2,cid);
            preparedStatement.setInt(3,hid);
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void updateStudent(Student student)  {
        connection = JDBCUtil.getInstance().getConnection();
        try {
            preparedStatement = connection.prepareStatement(" update student set `sname`=?,`cid`=?,`hid`=? where sid=?");
            preparedStatement.setString(1,student.getSname());
            preparedStatement.setInt(2,student.getCid());
            preparedStatement.setInt(3,student.getHid());
            preparedStatement.setInt(4,student.getSid());
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


}
